package com.wuzhishan.news.service.Impl;

import com.wuzhishan.news.mapper.articleMapper;
import com.wuzhishan.news.mapper.commentMapper;
import com.wuzhishan.news.mapper.userMapper;
import com.wuzhishan.news.pojo.BlogArticle;
import com.wuzhishan.news.pojo.BlogComment;
import com.wuzhishan.news.pojo.BlogUser;
import com.wuzhishan.news.service.otherService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.web.servlet.ModelAndView;

import java.util.List;

@Service
public class otherServiceImpl implements otherService {
    @Autowired
    private userMapper userMapper;
    @Autowired
    private articleMapper articleMapper;

    /**
     * 获取他人主页信息（用户信息，用户文章信息）
     * @param userId
     * @param mv
     * @param currentPage
     * @return
     */
    @Override
    public ModelAndView otherIndex(Integer userId, ModelAndView mv, Integer currentPage) {
        //默认每页显示五条数据
        int pageSize = 5;
        int pageIndex = (currentPage - 1) * pageSize;
        //查找对应用户的文章信息，并返回给视图
        Pageable pageable = new PageRequest((int) (currentPage - 1), pageSize);
        List<BlogArticle> articles = articleMapper.getOtherArticle(userId,pageIndex,pageSize);
        Page<BlogArticle> data = new PageImpl(articles,pageable,articles.size());
        mv.addObject("data",data);
        //查找对应用户文章信息的总数量，并返回给视图
        int totalPage = articleMapper.countArticleOfUser(userId);
        mv.addObject("totalPage",totalPage);
        //返回当前页数给视图
        mv.addObject("currentPage",currentPage);
        //查找对应id的用户信息，并添加到视图中
        BlogUser user = userMapper.getOneById(userId);
        mv.addObject("user",user);
        return mv;
    }
}
